module et {
    export class ObjectPool{
        private static _Instance:ObjectPool;
        public static get Instance(){
            if(!this._Instance){
                this._Instance=new ObjectPool();
            }
            return this._Instance;
        }
        private readonly  dictionary:Map<any,Array<any>> = new Map<any,Array<any>>();
        private readonly queuePool=new APool<Array<any>>();
        public fetch<T>(type:{new():T}):T
        {
            let queue=this.dictionary.get(type.prototype);
            let obj;
            if(queue==null){
                obj=new type();
            }
	        else if (queue.length == 0)
            {
	            obj =new type();
            }
            else
            {
                obj = queue.shift();
                //obj =new type();
            }
	        
            obj.isFromPool = true;
            obj.enabled=true;
            return obj;
        }
        
        public recycle(obj:any)
        {
            let type = Object.getPrototypeOf(obj);
	        let queue=this.dictionary.get(type);
            if (queue==null)
            {
                queue = this.queuePool.fetch(Array);            

				this.dictionary.set(type, queue);
            }
            queue.unshift(obj);
        }

	    public dispose():void
	    {
		    for (let kv of this.dictionary)
		    {
			    kv[1].length=0;
                this.queuePool.recycle(kv[1]);
		    }
		    this.dictionary.clear();
	    }
    }
}